//
// Created by chen on 2023/5/14.
//

#include "workerManager.h"

WorkerManager::WorkerManager() {
    // 初始化
    ifstream ifs;
    ifs.open(FILENAME, ios::in);
    if(!ifs.is_open()){
//        cout << "文件不存在" << endl;
        this -> empNum = 0;
        this -> empArray = nullptr;
        this ->fileIsEmpty = true;
        ifs.close();
        return;
    }
    char ch;
    ifs >> ch;
    if(ifs.eof()){
//        cout << "文件为空" << endl;
        this -> empNum = 0;
        this -> empArray = nullptr;
        this ->fileIsEmpty = true;
        ifs.close();
        return;
    }
    this->empNum = this->getEmpNum();
    this->empArray = new Worker * [this->empNum];
    this->fileIsEmpty = false;
    this->initEmp();
//    cout << "文件中有" <<this->empNum << "位员工"<< endl;
}


WorkerManager::~WorkerManager() {
    // 释放堆区数据
    if(this->empArray!= nullptr){
        for (int i = 0; i < this->empNum; ++i) {
            if(this->empArray[i] != nullptr){
                delete this->empArray[i];
                this->empArray[i] = nullptr;
            }
        }
        delete[] this->empArray;
        this->empArray = nullptr;
    }
}

void WorkerManager::showMenu() {
    cout << "**************************************" << endl;
    cout << "************欢迎使用员工管理系统*********" << endl;
    cout << "*************1、退出管理系统************" << endl;
    cout << "*************2、增加职工信息************" << endl;
    cout << "*************3、显示职工信息************" << endl;
    cout << "*************4、删除离职职工************" << endl;
    cout << "*************5、修改职工信息************" << endl;
    cout << "*************6、查找职工信息************" << endl;
    cout << "*************7、按照编号排序************" << endl;
    cout << "*************8、清空所有文档************" << endl;
    cout << "**************************************" << endl;
}

void WorkerManager::exitSystem(){
    cout << "欢迎下次再使用" << endl;
    exit(0);
}

void WorkerManager::saveToFile() {
    ofstream ofs;
    ofs.open(FILENAME, ios::out);
    for (int i = 0; i < this->empNum; ++i) {
        ofs << this->empArray[i]->m_id <<" "<<this->empArray[i]->m_name<<" "<<this->empArray[i]->m_deptId<<endl;
    }
    ofs.close();
}

void WorkerManager::addEmp() {
    cout << "输入本次添加员工的数量:";
    int num = 0;
    cin >> num;
    if(num >0){
        // 计算新空间大小
        int newSize = this->empNum + num;
        // 重新分配空间
        Worker ** newPointer = new Worker * [newSize];
        int index = 0;
        if(this->empArray != nullptr){    // 证明以前数组中有数据
            for (; index < this->empNum; ++index) {
                newPointer[index] = this->empArray[index];   // 将以前数组中数据放入新数组中
            }
        }
        for (int i = 0; i < num; ++i) {
            int id;
            string name;
            int dId;
            cout << "请输入第" << i+1 <<"个新员工编号:";
            cin >> id;
            cout << "请输入第" << i+1 <<"个新员工姓名:";
            cin >> name;
            cout << "请输入第" << i+1 <<"个新员工岗位(老板=1，经理=2，普通雇员=3):";
            cin >> dId;

            Worker * worker = nullptr;
            switch (dId) {
                case 1:
                    worker = new Boss(id, name, 1);
                    break;
                case 2:
                    worker = new Manager(id, name, 2);
                    break;
                case 3:
                    worker = new Staff(id, name, 3);
                    break;
                default:
                    break;
            }
            newPointer[this->empNum+i] = worker;
        }
        // 先释放原空间
        delete[] this->empArray;

        // 更改指向
        this->empArray = newPointer;

        // 更新个数
        this->empNum = newSize;

        cout << "成功添加" << num << "名员工" << endl;

        this->saveToFile();

        // 更新文件标志
        this->fileIsEmpty = false;
    }else {
        cout << "输入有误" << endl;
    }
}

int WorkerManager::getEmpNum() {
    ifstream ifs;
    ifs.open(FILENAME, ios::in);
    int id;
    string name;
    int dId;
    int num = 0;
    while (ifs>>id && ifs>>name && ifs>>dId){
        num++;
    }
    ifs.close();
    return num;
}

void WorkerManager::initEmp() {
    ifstream ifs;
    ifs.open(FILENAME, ios::in);
    int id;
    string name;
    int dId;
    int index=0;
    while (ifs>>id && ifs>>name && ifs>>dId){
        Worker * worker = nullptr;
        if(dId==1){
            worker = new Boss(id, name, dId);
        }else if(dId == 2){
            worker = new Manager(id, name, dId);
        } else if(dId == 3){
            worker = new Staff(id, name, dId);
        } else{
            cout << "出错了，dId=" << dId << endl;
        }
        this->empArray[index++] = worker;
    }
}

void WorkerManager::showEmp() {
    if(this->empNum == 0){
        cout << "记录为空!" << endl;
    } else{
        for (int i = 0; i < this->empNum; ++i) {
            this->empArray[i]->showInfo();
        }
    }
}

int WorkerManager::isExist(int id) {
    if(this->empNum ==0){
        return -1;
    }
    for (int i = 0; i < this->empNum; ++i) {
        if(this->empArray[i]->m_id == id){
            return i;
        }
    }
    return  -1;
}

void WorkerManager::deleteEmp() {
    int id=-1;
    cout << "输入要删除的员工编号:";
    cin >> id;
    if(this->empNum == 0) {
        cout << "员工列表为空" << endl;
        return;
    }
    int index = this->isExist(id);
    if(index == -1){
        cout << "职工不存在" << endl;
        return;
    } else{
        if(index == this->empNum-1){   // 判断是否为最后一位元素
            this->empArray[index] = nullptr;
        } else{
            for (int i = index; i < this->empNum-1; ++i) {
                this->empArray[i] = this->empArray[i+1];
            }
        }
        this->empNum--;
        this->saveToFile();
    }
    cout << "删除成功" << endl;
}

void WorkerManager::modifyEmp() {
    int id = -1;
    cout << "输入要修改的职工编号:";
    cin >> id;
    if(this->empNum == 0){
        cout << "记录为空" << endl;
        return;
    }
    int index = this->isExist(id);
    if(index==-1){
        cout << "要修改的员工不存在" << endl;
        return;
    }
    delete this->empArray[index];  // 释放老空间
    int newId = 0;
    string name;
    int dId;
    cout << "请输入新编号:";
    cin >> newId;
    cout << "请输入新姓名:";
    cin >> name;
    cout << "请输入新岗位((老板=1，经理=2，普通雇员=3)):";
    cin >> dId;
    Worker * worker = nullptr;
    if(dId==1){
        worker = new Boss(newId, name, dId);
    } else if(dId == 2){
        worker = new Manager(newId, name, dId);
    } else if(dId == 3){
        worker = new Staff(newId, name, dId);
    }
    this->empArray[index] = worker;
    this->saveToFile();
    cout << "更新成功" << endl;
}

void WorkerManager::findEmp() {
    int selectMod = -1;
    cout <<"请输入查找方式(1=编号，2=姓名):";
    cin >> selectMod;
    bool isFind = false;
    if(selectMod==1){
        int id=-1;
        cout << "输入要查找的员工编号:";
        cin >> id;
        if(this->empNum == 0) {
            cout << "员工列表为空" << endl;
            return;
        }
        int index = this->isExist(id);
        if(index == -1){
            cout << "员工不存在" << endl;
            return;
        } else{
            this->empArray[index]->showInfo();
        }
    } else if(selectMod == 2){
        string name="";
        cout << "输入要查找的员工姓名:";
        cin >> name;
        if(this->empNum == 0) {
            cout << "员工列表为空" << endl;
            return;
        }
        for (int i = 0; i < this->empNum; ++i) {
            if(this->empArray[i]->m_name == name){
                isFind = true;
                this->empArray[i]->showInfo();
            }
        }
        if(!isFind){
            cout << "查无此人" << endl;
        }
    } else{
        cout << "输入有误" << endl;
        return;
    }
}

void WorkerManager::sortEmp() {
    if(this->empNum == 0){
        cout << "员工列表为空" << endl;
        return;
    }
    int selectMod = -1;
    cout <<"请输入排序方式(1=升序，2=降序):";
    cin >> selectMod;
    for (int i = 0; i < this->empNum; ++i) {
        int index=i;
        for (int j = i+1; j < this->empNum; ++j) {
            if(selectMod == 1){
                if(this->empArray[index]->m_id > this->empArray[j]->m_id){
                    index = j;
                }
            }
            if(selectMod == 2){
                if(this->empArray[index]->m_id < this->empArray[j]->m_id){
                    index = j;
                }
            }
        }
        if(index != i){
            Worker * temp = this->empArray[index];
            this->empArray[index] = this->empArray[i];
            this->empArray[i] = temp;
        }
    }
    cout << "排序成功" << endl;
    this->saveToFile();
}

void WorkerManager::cleanFile() {
    int selectMod = -1;
    cout <<"请再次确认是否删除(1=删除，2=不删除):";
    cin >> selectMod;
    if(selectMod!=1){
        return;
    }
    for (int i = 0; i < this->empNum; ++i) {
        if(this->empArray[i] != nullptr){
            delete this->empArray[i];
            this->empArray[i] = nullptr;
        }
    }
    delete[] this->empArray;
    this->empNum = 0;
    this->fileIsEmpty = true;
    ofstream ofs;
    // ios::trunc 如果文件存在，则删除文件并重新创建
    ofs.open(FILENAME, ios::trunc);
    ofs.close();
    cout << "清空成功" << endl;
}

